<?php

class rutes extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('user_login/user', 'refresh');
        }

//        $this->load->model('admin_bank_model');
        $this->load->model('rute');
        $this->load->model('user_role');
        $this->load->library('user_id');
        $this->load->helper('user_helper');
    }

    function index() {
        $data['id'] = $this->user_id->id();
        
        $data['i'] = 1;
        $config = array();
        $config["base_url"] = base_url() . "rutes/index";
        $config["total_rows"] = $this->rute->record_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;
//
        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data["rows"] = $this->rute->fetch_costumer($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
        
        $data['content'] = ('rutes/index');
        $this->load->view('template/user', $data);
    }

    function add() {
        $data['id'] = $this->user_id->id();
        
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['options'] = $this->rute->get_array();
        $data['content'] = 'rutes/new';
        $this->load->view('template/user', $data);
    }

    function create() {
        $from = $this->input->post('from', TRUE);
        $to = $this->input->post('to', TRUE);
        $date = date('Y-m-d');
        $this->rute->create($from, $to, $date);
    }

    function edit() {
        $data['id'] = $this->user_id->id();
        
        $id = $this->uri->segment(3);
        //cek
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'bank_edit');

        $data['access'] = $access;
        $data['options'] = $this->rute->get_array();
        $data['rows'] = $this->rute->edit($id);
        $data['content'] = 'rutes/edit';
        $this->load->view('template/user', $data);
    }

    function update() {
        $id = $this->input->post('id', TRUE);
        $from = $this->input->post('from', TRUE);
        $to = $this->input->post('to', TRUE);
        $this->rute->update($id, $from, $to);
    }

    function delete() {
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'bank_delete');
        $id = $this->uri->segment(3);
        $this->rute->delete($id);
    }

    function find() {
        $bandara_a = $this->input->post('bandara_a', TRUE);
        $bandara_t = $this->input->post('bandara_t', TRUE);
        $bandara_t = $this->input->post('bandara_t', TRUE);
        $bandara_t = explode(' ', $bandara_t);
        $count = count($bandara_t);

        if ($count == 2)
            $bandara_t = $bandara_t[0] . "%20" . $bandara_t[1];
        else {
            $bandara_t = $bandara_t[0];
        }
        $date = $this->input->post('date', TRUE);
        $date = explode('/', $date);
        $data['bandara_a'] = $bandara_a;
        $data['bandara_t'] = $bandara_t;
        $data['date'] = $date[1] . $date[0];
        $data['content'] = ('rutes/find');
        $this->load->view('template/rute', $data);
    }

}

